package com.wust.diary;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.wust.database.DatabaseFactory;
import com.wust.util.ConstDefine;

public class DiaryDao {
	private Context context;
	private List<Diary> diarylist;
	public DiaryDao(Context context)
	{
		this.context=context;
	}
	public void insertDiary(Diary diary)
	{
		System.out.println("insert");
		DatabaseFactory database=new DatabaseFactory(context,ConstDefine.DatabaseFactoryUtil.DATABASE_NAME);
		SQLiteDatabase db=database.getWritableDatabase();
		ContentValues value=new ContentValues();
		value.put("diary_title", diary.getDiary_title());
		value.put("diary_content", diary.getDiary_content());
		value.put("diary_date", diary.getDiary_date());
		value.put("diary_mood", diary.getDiary_mood());
		value.put("user_name", diary.getUser_name());
		db.insert(ConstDefine.DatabaseFactoryUtil.DIARY_TABLE, null, value);
		db.close();
	}
	public List<Diary>queryDiaryList(String user_name,String time)
	{
		System.out.println("query diary");
		DatabaseFactory database=new DatabaseFactory(context,ConstDefine.DatabaseFactoryUtil.DATABASE_NAME);
		SQLiteDatabase db=database.getReadableDatabase();
		Cursor cursor=db.query(ConstDefine.DatabaseFactoryUtil.DIARY_TABLE, new String[]{"diary_id","diary_title","diary_content","diary_date","diary_mood","user_name"}, "user_name='"+user_name+"' and diary_date like '%"+time+"%'", null, null, null, "diary_date desc");
		diarylist=new ArrayList<Diary>();
		while(cursor.moveToNext())
		{
			Diary diary=new Diary();
			diary.setDiary_id(cursor.getInt(cursor.getColumnIndex("diary_id")));
			diary.setDiary_title(cursor.getString(cursor.getColumnIndex("diary_title")));
			diary.setDiary_content(cursor.getString(cursor.getColumnIndex("diary_content")));
			diary.setDiary_date(cursor.getString(cursor.getColumnIndex("diary_date")));
			diary.setDiary_mood(cursor.getInt(cursor.getColumnIndex("diary_mood")));
			diarylist.add(diary);
		}
		cursor.close();
		db.close();
		return diarylist;
	}
	public Diary getDiary(String time,String user_name,String diaryTitle)
	{
		DatabaseFactory database=new DatabaseFactory(context,ConstDefine.DatabaseFactoryUtil.DATABASE_NAME);
		SQLiteDatabase db=database.getReadableDatabase();
		Cursor cursor=db.query(ConstDefine.DatabaseFactoryUtil.DIARY_TABLE, new String[]{"diary_id","diary_title","diary_content","diary_date","diary_mood","user_name"}, "user_name=? and diary_date like ? and diary_title=?", new String[]{user_name,time+"%",diaryTitle}, null, null, "diaryDate desc");
		Diary diary=new Diary();
		while(cursor.moveToNext())
		{
			diary.setDiary_id(cursor.getInt(cursor.getColumnIndex("diary_id")));
			diary.setDiary_title(cursor.getString(cursor.getColumnIndex("diary_title")));
			diary.setDiary_content(cursor.getString(cursor.getColumnIndex("diary_content")));
			diary.setDiary_date(cursor.getString(cursor.getColumnIndex("diary_date")));
			diary.setDiary_mood(cursor.getInt(cursor.getColumnIndex("diary_mood")));
			diarylist.add(diary);
		}
		cursor.close();
		db.close();
		return diary;
	}
	public void deleteDiary(String diary_id)
	{
		System.out.println("delete"+diary_id);
		DatabaseFactory database=new DatabaseFactory(context,ConstDefine.DatabaseFactoryUtil.DATABASE_NAME);
		SQLiteDatabase db=database.getWritableDatabase();
		String sql="delete from da_diary where diary_id="+diary_id;
		db.execSQL(sql);
		db.close();
	}
}
